package com.zhentao.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.login.mapper.TMenuMapper;
import com.zhentao.login.service.TMenuService;
import com.zhentao.login.service.TUserService;
import com.zhentao.login.utils.AuthContextUtil;
import com.zhentao.nlz.project.login.TMenu;
import com.zhentao.nlz.project.product.vo.MenuVo;
import com.zhentao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import com.zhentao.nlz.project.login.TUser;
import com.zhentao.nlz.project.product.vo.MenuVo;
import com.zhentao.utils.MenuHelper;
import com.zhentao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
* @author 不.凌
* @description 针对表【t_menu(菜单表)】的数据库操作Service实现
* @createDate 2024-09-22 20:51:43
*/
@Service
public class TMenuServiceImpl extends ServiceImpl<TMenuMapper, TMenu>
    implements TMenuService {
    @Autowired
    private TMenuMapper menuMapper;
    @Autowired
    private TUserService userService;


    @Override
    public Result getMenu() {
        QueryWrapper<TMenu> wrapper = new QueryWrapper<>();
        List<TMenu> tMenus = menuMapper.selectList(wrapper);
        List<TMenu> treeList = MenuHelper.buildTree(tMenus); //构建树形数据
        return Result.success(tMenus);
    }

    @Override
    public Result saveMenu(TMenu menu) {
        menu.setCreateTime(new Date());
        boolean save = this.save(menu);
        return Result.success(save);
    }

    @Override
    public Result updateMenu(TMenu menu) {
        menu.setUpdateTime(new Date());
        boolean update = this.updateById(menu);
        return Result.success(update);
    }

    @Override
    public Result removeMenu(Integer menuId) {
        boolean removeById = this.removeById(menuId);
        return Result.success(removeById);
    }

    @Override
    public List<MenuVo> findUserMenuList() {

        TUser sysUser = AuthContextUtil.getUser();
        Integer userId = sysUser.getUserId();          // 获取当前登录用户的id

        List<TMenu> sysMenuList = menuMapper.selectListByUserId(userId) ;

        //构建树形数据
        List<TMenu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);
        return this.buildMenus(sysMenuTreeList);
    }

    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<MenuVo> buildMenus(List<TMenu> menus) {

        List<MenuVo> sysMenuVoList = new LinkedList<MenuVo>();
        for (TMenu sysMenu : menus) {
            MenuVo sysMenuVo = new MenuVo();
            sysMenuVo.setTitle(sysMenu.getName());
            sysMenuVo.setName(sysMenu.getPermission());
            List<TMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }




    }





